﻿// -----------------------------------------------------------------------------
// 园丁,是个很简单的管理系统
//  gitee:https://gitee.com/hgflydream/Gardener 
//  issues:https://gitee.com/hgflydream/Gardener/issues 
// -----------------------------------------------------------------------------

using Gardener.Core.Common;
using Gardener.Weighbridge.Impl.Entities;
using Gardener.Weighbridge.Services;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Gardener.Weighbridge.Impl.Services
{
    /// <summary>
    /// 称重记录服务
    /// </summary>
    [ApiDescriptionSettings("Weighbridge", Module = "weighbridge")]
    public class WeighingRecordService : ServiceBase<WeighingRecord, WeighingRecordDto, Guid, GardenerMultiTenantDbContextLocator>, IWeighingRecordService
    {

        /// <summary>
        /// 称重记录服务
        /// </summary>
        /// <param name="repository"></param>
        public WeighingRecordService(IRepository<WeighingRecord, GardenerMultiTenantDbContextLocator> repository) : base(repository)
        {
        }
        /// <summary>
        /// 根据车牌号获取最新记录
        /// </summary>
        /// <remarks>
        /// 根据车牌号获取最新称重记录，如果没有返回null
        /// </remarks>
        /// <param name="plateNumber">车牌号</param>
        /// <returns></returns>
        public Task<WeighingRecordDto?> FindLastByPlateNumber(string plateNumber)
        {
            return _repository.AsQueryable(false).Where(x => x.PlateNumber.Equals(plateNumber)).OrderByDescending(x => x.CreatedTime).Select(x => x.Adapt<WeighingRecordDto>()).FirstOrDefaultAsync();
        }
    }
}
